<?php

namespace app\api\controller\v1;

use app\admin\model\OcationClass;
use app\common\controller\Api;
use think\Request;
use app\admin\model\Addresslv;
use app\admin\model\Classes;
use app\admin\model\Student;

class ClassTeam extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getClassTeamInfo(Request $request)
    {
        $params = $request->param();
        if (empty($params['rel_type']) || empty($params['rel_id'])){
            $this->error('参数错误',[], 400);
        }

        $Addresslv = Addresslv::where('rel_type', $params['rel_type'])
            ->where('rel_id' ,$params['rel_id'])
            ->where('status' ,1)
            ->where('pid','<>',0)
            ->field('id,name')
            ->select();
        $class = Classes::where('rel_id',$params['rel_id'])
            ->where('classes_type', $params['rel_type'])
            ->where('status', 1)
            ->field('id,name,addresslv_id')
            ->select();

        $data = [];
        if (!empty($Addresslv) && !empty($class)){
            foreach ($Addresslv as $k => $val){
                $data[$k]['name'] = $val['name'];
                foreach ($class as $v){
                    if ($val['id'] === $v['addresslv_id']){
                        $data[$k]['children'][] = ['id'=>$v['id'], 'name'=>$v['name']];
                    }
                }
            }
        }

        $this->success('获取成功', $data, 200);
    }

    public function applyJoinClass(Request $request)
    {
        $classes_id = $request->get('classes_id');
        $classes_type = $request->get('classes_type');
        $user = $this->auth->getUser();

        if (empty($user['uid'])){
            return $this->error('请先登录', [], 401);
        }
        if (empty($classes_id)){
            $this->error('参数有误',[], 400);
        }
        if (empty($classes_type)){
            $this->error('参数有误',[], 400);
        }
        if ($classes_type === 1){
            $class = Classes::where('id',$classes_id)->find();
            if (empty($class)){
                $this->error('暂无相关信息',[], 400);
            }

            $query = Student::where('status', '<>', -1)
                ->where('school_id',$class['school_id'])
                ->where('classes_id',$classes_id)
                ->where('status', '<>', 2);

            $student = $query->where('student_id',$user['uid'])->find();

            if (!empty($student)){
                $this->error('您已申请过了', $student, 400);
            }
        }elseif ($classes_type === 2){
            $class = OcationClass::where('id',$classes_id)->find();
            if (empty($class)){
                $this->error('暂无相关信息',[], 400);
            }

            $query = Student::where('status', '<>', -1)
                ->where('school_id',$class['school_id'])
                ->where('classes_id',$classes_id)
                ->where('status', '<>', 2);

            $student = $query->where('student_id',$user['uid'])->find();

            if (!empty($student))
                $this->error('您已申请过了', $student, 400);
        }


        $this->success('申请成功', $query->insert([
            'classes_id'=>$class['id'],
            'school_id'=>$class['school_id'],
            'classes_type'=>$class['classes_type'],
            'student_id'=>$user['uid'],
            'create_time'=> date('Y-m-d H:i:s'),
            'update_time'=> date('Y-m-d H:i:s'),
            'status'=> $class['classes_type'] === 2 ? 1 : 0,
            'add_time'=> time()
        ]), 200);
    }
}
